// perl_recorder.cpp - macro recorder

#include "PlPN.h"

/*
 * PerlRecorder
 */
 
PerlRecorder::PerlRecorder() {}

PerlRecorder::~PerlRecorder() {}

void PerlRecorder::RecordScintillaAction(int message, WPARAM wParam, LPARAM lParam) {	
	// call the event for every perl sink we're tracking
	for (int i = 0; i < m_perl_sinks.size(); i++) {	
		dSP;

		ENTER;
		SAVETMPS;

		PUSHMARK(SP);
		XPUSHs(m_perl_sinks[i]);
		XPUSHs(sv_2mortal(newSViv(message)));
		XPUSHs(sv_2mortal(newSVuv(wParam)));
		XPUSHs(sv_2mortal(newSVuv(lParam)));
		PUTBACK;
	
		call_method("RecordScintillaAction", G_VOID|G_EVAL);
		SPAGAIN;

		if(SvTRUE(ERRSV)) {
			STRLEN n_a;
			alert("Error calling Perl version of RecordScintillaAction: %s\n", SvPV(ERRSV,n_a));
			POPs;
		}
	
		PUTBACK;
		FREETMPS;
		LEAVE;
	}

	return;
}

void PerlRecorder::RecordSearchAction(SearchType type, const extensions::ISearchOptions* options, FindNextResult result) {
	SV* optionssv = create_perl_object((void*)options, "PlPN::SearchOptions");

	// call the event for every perl sink we're tracking
	for (int i = 0; i < m_perl_sinks.size(); i++) {	
		dSP;

		ENTER;
		SAVETMPS;

		PUSHMARK(SP);
		XPUSHs(m_perl_sinks[i]);
		XPUSHs(sv_2mortal(newSViv(type)));
		XPUSHs(optionssv);
		XPUSHs(sv_2mortal(newSViv(result)));
		PUTBACK;
	
		call_method("RecordSearchAction", G_VOID|G_EVAL);
		SPAGAIN;

		if(SvTRUE(ERRSV)) {
			STRLEN n_a;
			alert("Error calling Perl version of RecordSearchAction: %s\n", SvPV(ERRSV,n_a));
			POPs;
		}
	
		PUTBACK;
		FREETMPS;
		LEAVE;
	}

	sv_2mortal(optionssv);

	return;
}

void PerlRecorder::StartRecording() {	
	// call the event for every perl sink we're tracking
	for (int i = 0; i < m_perl_sinks.size(); i++) {
		dSP;

		ENTER;
		SAVETMPS;

		PUSHMARK(SP);
		XPUSHs(m_perl_sinks[i]);
		PUTBACK;
	
		call_method("StartRecording", G_VOID|G_EVAL);
		SPAGAIN;

		if(SvTRUE(ERRSV)) {
			STRLEN n_a;
			alert("Error calling Perl version of StartRecording: %s\n", SvPV(ERRSV,n_a));
			POPs;
		}
	
		PUTBACK;
		FREETMPS;
		LEAVE;
	}
	
	return;
}

void PerlRecorder::StopRecording() {	
	// call the event for every perl sink we're tracking
	for (int i = 0; i < m_perl_sinks.size(); i++) {
		dSP;

		ENTER;
		SAVETMPS;

		PUSHMARK(SP);
		XPUSHs(m_perl_sinks[i]);
		PUTBACK;
	
		call_method("StopRecording", G_VOID|G_EVAL);
		SPAGAIN;

		if(SvTRUE(ERRSV)) {
			STRLEN n_a;
			alert("Error calling Perl version of StopRecording: %s\n", SvPV(ERRSV,n_a));
			POPs;
		}
	
		PUTBACK;
		FREETMPS;
		LEAVE;
	}
	
	return;
}
